<?php
	// require("php/akses.php");
	require("php/managesession.php");
	
	function newline() {
		echo "</br>";
	}
	
	function isJustReg() {
		return (isset($_FILES["myImage"]['name']));
	}
	
	function getMyImage() {
		return $_FILES["myImage"]['name'];
	}
	
	function getUsername() {
		return $_POST["text_username"];
	}
	
	function getPassword() {
		return $_POST["text_password"];
	}
	
	function getConfirmPassword() {
		return $_POST["text_confirm"];
	}
	
	function getFullname() {
		return $_POST["text_fullname"];
	}
	
	function getBirthday() {
		return $_POST["SelectedDate"];
	}
	
	function getEmail() {
		return $_POST["text_email"];
	}
	
	function getGender() {
		return $_POST["select_gender"];
	}
	
	function getAbout() {
		return $_POST["text_aboutme"];
	}
	
	function saveFile($uname) {
		if ($_FILES["myImage"]["error"] > 0) {
			return "Return Code: " . $_FILES["myImage"]["error"] . "<br />";
		} else {
			$avatarfolder = "images/avatar/";
			$savefilename = $uname."."."jpg";
			move_uploaded_file($_FILES["myImage"]["tmp_name"],
			$avatarfolder . $savefilename);
			return $avatarfolder . $savefilename;
		}
	}
	
	function setTagArray($tagsstr) {
		$rawtags = explode(",",$tagsstr);
		$res = array();
		foreach ($rawtags as $atag) {
			$atag = trim($atag);
			if ($atag!="") {
				array_push($res, $atag);
			}
		}
		return $res;
	}
	
	function allValidation(&$errors, &$valid, &$lastinput){
		/*Validate Username*/
		connect();
		$user;
		$valid = true;
		$user["username"]=getUsername();
		$lastinput[1] = $user["username"];
		$user["username"] = trim($user["username"]);
		if(strlen($user["username"])<5){
				$errors[0] = "Your username must be at least 5 characters";
				$valid = false;
		}
		else{
			if (!isUsernameUnique($user["username"])) {
				$errors[0] =  "This username is already registered.";
				$valid = false;
			} else {
				$errors[0] =  "OK";
			}
			if($user["username"]==getPassword()){
				$errors[1] =  "Your password must be different from your username";
				$valid = false;
			}
		}
		
		/*Validate Password*/
		$user["password"]=getPassword();
		$lastinput[2] = $user["password"];
		if(strlen($user["password"])<8){
			if ($errors[1] == "") {
				$errors[1] =  "Your Password must be at least 8 digits";
			}
			$valid = false;
		}
		
		/*Validate Confirm Password*/
		$pp=getPassword();
		$cc=getConfirmPassword();
		$lastinput[3] = $cc;
		if($cc!=$pp){
			$errors[2] =  "Password confirmation is wrong";
			$valid = false;
		} else {
			if ($cc == "") {
				$errors[2] =  "Password Confirmation can't be empty";
				$valid = false;
			}
		}
		
		/*Validate Full Name*/
		$user["nama_lengkap"]=getFullname();
		$lastinput[4] = $user["nama_lengkap"];
		$user["nama_lengkap"] = trim($user["nama_lengkap"]);
		$atn=strpos($user["nama_lengkap"]," ");
		if($atn==-1 || $user["nama_lengkap"]==""){
			$errors[3] =  "Your full name must contain at least first & last name";
			$valid = false;
		}

		/*Validate Email*/
		$user["email"]=getEmail();
		$lastinput[6] = $user["email"];
		$user["email"] = trim($user["email"]);
		$atpos=strpos($user["email"],"@");
		$dotpos=strripos($user["email"],".");
		if ($atpos<1 || $dotpos<$atpos+2 || $dotpos+2>=strlen($user["email"]))
		{
			$errors[4] =  "Not a valid e-mail address";
			$valid = false;
		} else {
			if (!isEmailUnique($user["email"])) {
				$errors[4] =  "This email is already registered.";
				$valid = false;
			}
		}
		
		if($user["password"]==$user["email"]){
			if ($errors[1] == "") {
				$errors[1] =  "Your password must be different from your email";
				$valid = false;
			}
		}
		
		/*Validate Gender*/
		$user["gender"]=getGender();
		$lastinput[7] = $user["gender"];
		if($user["gender"]=="Choose one"){
			$errors[5] =  "You must choose one gender";
			$valid = false;
		} else {
			if ($user["gender"]=="Male") {
				$user["gender"]='L';
			} else {
				$user["gender"]='P';
			}
		}
		
		/*Validation Photo*/
		$ft=getMyImage();
		$lastinput[0] = $ft;
		if($ft!="" && (!strpos($ft,'jpg') && !strpos($ft,'jpeg') && !strpos($ft,'JPG') && !strpos($ft,'JPEG'))){
			$errors[6] = "Your photo must have jpeg or jpg file type";
			$valid = false;
		}
		
		/* Validation Date */
		$user["tanggal_lahir"]=trim(getBirthday());
		$lastinput[5] = $user["tanggal_lahir"];
		if ($user["tanggal_lahir"]=="") {
			$errors[7] = "Your birthday can't be empty";
			$valid = false;
		} else {
			if(!isDate($user["tanggal_lahir"])){
				$errors[7] = "Invalid date, make sure the format is YYYY-MM-DD";
				$valid = false;
			}
		}
		
		if ($errors[1]=="") {
			$errors[1]="OK";
		}
		
		$user["about"] = getAbout();
		$lastinput[8] = getAbout();
		//saveFile();
		if ($valid) {
			$user["avatar_path"] = NULL;
			if ($ft!="") {
				$user["avatar_path"]=saveFile($user["username"]);
			} else {
				$user["avatar_path"]="images/default.jpg";
			}
			if (addUser($user)) {
				setLogin($user);
				if (isLogin()) {
					header('Location: profil.php?id='.getLoginUserid());
				}
			}
			
			// echo "Congratulation! U have registered!";
			// newline();
			
			/* validasi valid:
				saveFile (simpan avatar)
				masukkin database! 
				trus di login
				balik k profil
				jgn lupa - ini udah*/
		}
		//location.href = "daftarkonten.php" ;
	}
?>